// Project: 2550gaigar02
// FileName: main.c
#include <p18cxxx.h>
#include <timers.h>
#include <delays.h>
#pragma config FOSC=HSPLL_HS, PLLDIV=5, CPUDIV=OSC1_PLL2
#pragma config USBDIV=2, PWRT=OFF, FCMEN=OFF, IESO=OFF
#pragma config BOR=OFF, BORV=2, WDT=OFF, LVP=OFF,STVREN=ON
#pragma config MCLRE=OFF, PBADEN=OFF, WDTPS=1024
#pragma config LPT1OSC=OFF, CCP2MX=OFF, DEBUG=OFF
#pragma config CP0=OFF, CP1=OFF, CP2=OFF, CP3=OFF, CPB=OFF
#pragma config CPD=OFF, WRT0=OFF, WRT1=OFF, WRT2=OFF, WRT3=OFF
#pragma config WRTB=OFF, WRTC=OFF, WRTD=OFF, EBTR0=OFF
#pragma config EBTR1=OFF, EBTR2=OFF, EBTR3=OFF, EBTRB=OFF
#define lcd_DB LATB
#define lcd_E LATBbits.LATB3
#define lcd_RS LATBbits.LATB2
#define Delay_50uS Delay100TCYx(6)
#define Delay_200uS Delay100TCYx(24)
#define Delay_1mS Delay1KTCYx(12)
#define Delay_2mS Delay1KTCYx(24)
#define Delay_5mS Delay10KTCYx(6)
#define Delay_10mS Delay10KTCYx(12)
#define Delay_20mS Delay10KTCYx(24)
#define Delay_100mS Delay10KTCYx(120)
int A, AA, C, CC;
unsigned char N, sw, INT, s;
unsigned char b1,b2,b3,b4,b5,b6,b7,b8;
unsigned char t1,t2,t3,t4,t5,t6;
unsigned char c1,c2,c3,c4,c5,c6,c7,c8,c9,c10;
void UserInit(void);
void lcd_init(void);
void lcd_w4(char asci);
void lcd_c4(char cmd);
void lcd_clr(void);
void lcd_puts(char *s);
void lcd_locate(char y,char x);
void lcd_putchr(char c);
void lcd_putui(
unsigned int ui, unsigned char d);
void high();
void low();
#pragma interrupt high
#pragma interruptlow low
#pragma code H_vect = 0x0008
void H_isr(void){
_asm goto high _endasm
}
#pragma code L_vect = 0x0018
void L_isr(void){
_asm goto low _endasm
}
#pragma code
void high(){
if(PIR1bits.TMR1IF){
PIR1bits.TMR1IF = 0;
N++;
if(N > 24){
N = 0;
C = c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8 + c9 + c10;
c10 = c9; c9 = c8; c8 = c7; c7 = c6; c6 = c5;
c5 = c4; c4 = c3; c3 = c2; c2 = c1;
c1 = 0;
t1++;
if(t1 > 9){
t2++; t1 = 0;
}
if(t2 > 5){
t3++; t2 = 0; AA = A; A = 0;
}
if(t3 > 9){
t4++; t3 = 0;
}
if(t4 > 5){
t5++; t4 = 0;
}
if(t5 > 9){
t6++; t5 = 0;
}
if((t6 > 1)&&(t5 > 3)){
t5 = 0; t6 = 0;
}
}
TMR1H = 21; TMR1L = 175;
}
if(INTCONbits.INT0IF){
INTCONbits.INT0IF = 0;
INT = 1;
A++; b1++; c1++;
}
}
void low(){
if(INTCON3bits.INT1IF){
INTCON3bits.INT1IF = 0;
switch(sw){
case 0: sw = 1; break;
case 1: sw = 2; break;
case 2: sw = 0; break;
default: break;
}
Delay_20mS;
}
}
void lcd_w4(char c){
lcd_DB = (c & 0xF0) | (lcd_DB & 0x0F);
lcd_E = 1;
Delay10TCYx(1);
lcd_E = 0;
}
void lcd_c4(char c){
lcd_RS = 0;
lcd_w4(c);
lcd_w4(c<<4);
if(c & 0xFC)
Delay_50uS;
else
Delay_2mS;
}
void lcd_clr(void){
lcd_c4(0x01);
}
void lcd_locate(char y, char x){
unsigned char p;
switch(y & 0x03){
case 0: p=0x80; break;
case 1: p=0xC0; break;
case 2: p=0x94; break;
case 3: p=0xD4; break;
}
lcd_c4(p += x);
}
void lcd_putchr(char c){
lcd_RS = 1;
lcd_w4(c);
lcd_w4(c<<4);
Delay_50uS;
}
void lcd_puts(char *s){
while(*s)
lcd_putchr(*s++);
}
void lcd_putui(
unsigned int ui, unsigned char d){
unsigned char i;
unsigned char buf[5];
for(i = 0; i < 5; i++)
buf[i] = ' ';
i = 4;
do {
buf[i] = (ui % 10) + '0';
ui = ui / 10;
i--;
} while(ui > 0);
for(i = (5 - d); i < 5; i++)
lcd_putchr(buf[i]);
}
void lcd_init(void){
Delay_20mS;
lcd_RS = 0;
lcd_w4(0x30);Delay_5mS;
lcd_w4(0x30);Delay_1mS;
lcd_w4(0x30);Delay_1mS;
lcd_w4(0x20);Delay_1mS;
lcd_c4(0x2E);
lcd_c4(0x08);
lcd_c4(0x0C);
lcd_c4(0x06);
lcd_c4(0x01);
}
void UserInit(void){
N=0; sw = 0; INT = 0; s = 0;
A=0; AA=0;
b1=0; b2=0; b3=0; b4=0; b5=0; b6=0; b7=0; b8=0;
t1=0; t2=0; t3=0; t4=0; t5=0; t6=0;
c1=0; c2=0; c3=0; c4=0; c5=0;
c6=0; c7=0; c8=0; c9=0; c10=0; C=0; CC=0;
}
char blank[] = " ";
char colon[] = ":";
char count[] = " count";
char time[] = " time";
char cpm[] = " cpm";
char cpm2[] = " cpm2";
char cpmRT[] = " cpm\n";
void main(void){
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
TRISA = 0b00000000;
TRISB = 0b00000011;
TRISC = 0b10000000;
ADCON1 = 0xFF;
T2CON = 0b00000101;
PR2 = 180;
CCP1CON = 0b00001100;
CCPR1L = 3;
INTCONbits.INT0IE = 1;
INTCONbits.INT0IF = 0;
INTCON3bits.INT1IE = 1;
INTCON3bits.INT1IP = 0;
INTCON3bits.INT1IF = 0;
OpenTimer1(
TIMER_INT_ON &
T1_8BIT_RW &
T1_SOURCE_INT &
T1_PS_1_8 &
T1_OSC1EN_OFF &
T1_SYNC_EXT_OFF
);
IPR1bits.TMR1IP = 1;
PIR1bits.TMR1IF = 0;
RCONbits.IPEN = 1;
INTCONbits.GIEH = 1;
INTCONbits.GIEL = 1;
UserInit();
lcd_init();
while(1){
char i;
for(i=0;i<50;i++){
Delay_1mS;
}
if(b1 > 9){
b2++; b1 = 0;
}
if(b2 > 9){
b3++; b2 = 0;
}
if(b3 > 9){
b4++; b3 = 0;
}
if(b4 > 9){
b5++; b4 = 0;
}
if(b5 > 9){
b6++; b5 = 0;
}
if(b6 > 9){
b7++; b6 = 0;
}
if(b7 > 9){
b8++; b7 = 0;
}
if(sw == 0){
lcd_locate(0, 0);
lcd_puts(blank);
lcd_putui(b8, 1); lcd_putui(b7, 1);
lcd_putui(b6, 1); lcd_putui(b5, 1);
lcd_putui(b4, 1); lcd_putui(b3, 1);
lcd_putui(b2, 1); lcd_putui(b1, 1);
lcd_puts(count);
}
if(sw == 1){
lcd_clr();
lcd_locate(0, 4);
lcd_putui(AA, 5);
lcd_puts(cpm);
}
if(sw == 2){
CC = C*6;
lcd_clr();
lcd_locate(0, 4);
lcd_putui(CC, 5);
lcd_puts(cpm2);
}
lcd_locate(1, 0);
lcd_puts(blank);
lcd_putui(t6, 1); lcd_putui(t5, 1);
lcd_puts(colon);
lcd_putui(t4, 1); lcd_putui(t3, 1);
lcd_puts(colon);
lcd_putui(t2, 1); lcd_putui(t1, 1);
lcd_puts(time);
if(INT){
char j;
for(j = 0; j < 100; j++){
s = !s;
PORTAbits.RA3 = s;
Delay_1mS;
}
INT = 0;
s = 0;
}
}
}